package com.gxuwz.check.action.admin;

import com.alibaba.fastjson.JSONObject;
import com.gxuwz.check.base.BaseAction;
import com.gxuwz.check.model.dto.StudentDto;
import com.gxuwz.check.model.entity.SysStudentEntity;
import com.gxuwz.check.service.admin.IStudentService;
import com.gxuwz.check.util.ResultJson;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class StudentAction extends BaseAction implements ModelDriven<SysStudentEntity> {

    @Autowired
    private IStudentService studentService;

    private SysStudentEntity sysStudentEntity = new SysStudentEntity();

    @Override
    public SysStudentEntity getModel() {
        return sysStudentEntity;
    }
    private int page; //当前页码
    private int classId; //班级Id

    private Integer studentId;


    /**
     * 获取所有学生(可根据班级）
     */
    public void list() {
        List<StudentDto> list = studentService.list(page,classId,0);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        jsonObject.put("list",list);
        ResultJson.R(jsonObject);
    }

    /**
     * 删除学生
     */
    public void delete() {
        System.out.println("studentId  "+studentId);
        studentService.delete(sysStudentEntity.getStudentId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        ResultJson.R(jsonObject);
    }

    /**
     * 修改学生
     */
    public void edit() {

    }

    /**
     * 添加学生
     */
    public void save() {
        studentService.save(sysStudentEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        ResultJson.R(jsonObject);
    }

    public void findStudentById(){
        System.out.println("studentId  "+studentId);
        SysStudentEntity sysStudentEntity1 = studentService.findStudentById(sysStudentEntity.getStudentId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        jsonObject.put("info",sysStudentEntity1);
        ResultJson.R(jsonObject);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public IStudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }
}